home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / blankery / bserverdir / sources / server / bserver.c < prev    next >
C/C++ Source or Header  |  1994-11-29  |  13KB  |  546 lines

  1. #include <exec/types.h>
  2. #include <exec/memory.h>
  3. #include <intuition/intuition.h>
  4. #include <dos/dos.h>
  5.  
  6. #include <clib/exec_protos.h>
  7. #include <clib/alib_protos.h>
  8. #include <clib/intuition_protos.h>
  9. #include <clib/gadtools_protos.h>
  10. #include <clib/dos_protos.h>
  11. #include <clib/utility_protos.h>
  12. #include <clib/commodities_protos.h>
  13. #include <clib/alib_protos.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16.  
  17. #include "/include/server.h"
  18.  
  19. /* Pulizia di una porta messaggi */
  20. extern void ClearPendingMessages(struct MsgPort *);
  21.  
  22. /* Controllo commodity */
  23. extern BOOL SetUpCommodity(int,char**);
  24. extern void RemoveCommodity(void);
  25. extern void HandleCxMessages(void);
  26.  
  27. /* Gestione finestra */
  28. extern void DetachGadgets( void );
  29. extern void AttachGadgets( void );
  30. extern BOOL PopUpWindow(void);
  31. extern void ShutWindow(void);
  32. extern void HandleWindowMessages(void);
  33.  
  34. /* Builtin blanker */
  35. extern BOOL PopUpBlackScreen(void);
  36. extern void CloseBlackScreen(void);
  37.  
  38. /* Fa partire i clienti nella ClientList */
  39. extern BOOL StartClient( struct ClientNode * );
  40. extern void GetClientNames( char * );
  41. extern void DropClientNames( void );
  42.  
  43. /* Alloca una dinfo per un cliente */
  44. extern struct DisplayIDInformation *AllocDisplayIDInformation( ULONG );
  45. extern struct Window *window;
  46.  
  47. /* Per la trasmissione del displayID */
  48. extern ULONG DisplayID;
  49. extern char DefaultModeName[];
  50. extern struct List *CreateModeList( void );
  51. extern void GetDisplayNodeFromName( void );
  52. extern void DeleteModeList( void );
  53.  
  54. extern struct IntuitionBase *IntuitionBase;
  55. extern struct Library *IconBase, *GadToolsBase, *UtilityBase, *CxBase;
  56. extern struct Gadget *list_gdg;
  57.  
  58. char *version = "$VER: "PROGRAMNAME" "PROGRAMVERSION" ("PROGRAMDATE")";
  59.  
  60. /* Per la notifica dei vari eventi */
  61. struct MsgPort *handlerPort, *timerMPort, *clientMPort;
  62. struct Task *thisTask;
  63. UBYTE handlerSigBit, timerSigBit;
  64. ULONG handlerSignal, timerSignal;
  65. extern ULONG CXSignal, windowSignal, timerSignal;
  66.  
  67.  
  68. BOOL StayCool = TRUE;            /* Terminazione */
  69. BOOL Blanking = FALSE;
  70. BOOL RandomClient = TRUE;        /* Deve essere scelto a caso? */
  71. BOOL CommodityActive = TRUE;    /* Deve effettuare il blank? */
  72. BOOL PopUp = FALSE;                /* Deve aprire la finestra? */
  73. UWORD timeElapsed;                /* Tempo passato dall'ultimo evento */
  74. UWORD delaySecs, delayEvents;    /* Secondi da passare trasform. in Ticks */
  75. UWORD DefaultClient = 0;
  76. UWORD TotalClients = 0;    
  77.  
  78. #define MUST_RETRY 0
  79. #define MUST_BLANK 1
  80.  
  81. BOOL BlankStatus = MUST_RETRY;
  82.  
  83. /* Usate nella scelta del cliente per non toccare le originali  */
  84. /* In caso di fallimento infatti vanno modificate per scandire  */
  85. /* la lista dalla fine all'inizio alla ricerca di un nuovo cli: */
  86.  
  87. UWORD copy_of_DefaultClient = (UWORD)~0;
  88. BOOL copy_of_RandomClient = (UWORD)~0;
  89.  
  90. #define BLANKING_NONE      0
  91. #define BLANKING_BUILTIN   1
  92. #define BLANKING_EXTERN    2
  93.  
  94. /* Prototipi delle funzioni */
  95. struct ClientMessage *AllocClientMessage( ULONG );
  96. void FreeClientMessage( struct ClientMessage * );
  97. struct ClientNode *FindClientNode( UWORD );
  98. void RemoveClient( UWORD, char * );
  99.  
  100. void SendCommandToClient( ULONG );
  101. void QuitClients( void );
  102. void HandleClients(void);
  103. struct MsgPort *SetUpServerPort(void);
  104. void RemoveServerPort(void);
  105.  
  106. void StartBlanking(void);
  107. void StopBlanking(void);
  108.  
  109. void ProcessUserMessages(void);
  110. void main(int,char**);
  111.  
  112.  /*************************
  113.  *                        *
  114.  * HANDLE EXTERN PROGRAMS *
  115.  *                        *
  116.  *************************/
  117.  
  118. struct MsgPort *ServerPort;
  119. ULONG serverSignal;
  120. UBYTE blankingAction;
  121.  
  122. struct List ClientsList;
  123.  
  124.  
  125. struct ServerMessage *AllocServerMessage( ULONG command )
  126. {
  127. struct ServerMessage *bmsg;
  128.  
  129. if ( bmsg = AllocVec( sizeof(struct ServerMessage), MEMF_PUBLIC | MEMF_CLEAR ) )
  130.     {
  131.     bmsg->sm_Msg.mn_Node.ln_Type = NT_MESSAGE;
  132.     bmsg->sm_Msg.mn_ReplyPort = ServerPort;
  133.     bmsg->sm_Msg.mn_Length = sizeof(struct ServerMessage);
  134.     bmsg->sm_Command = command;
  135.     }
  136. return( bmsg );
  137. }
  138.  
  139.  
  140. void FreeServerMessage( struct ServerMessage *bmsg )
  141. {
  142. FreeVec( bmsg );
  143. }
  144.  
  145.  
  146. struct ClientNode *FindClientNode( UWORD clientNumber )
  147. {
  148. struct ClientNode *node = (struct ClientNode *)ClientsList.lh_Head;
  149. UWORD counter = 0;
  150.  
  151. while ( counter < clientNumber )
  152.     {
  153.     counter++;
  154.     node = (struct ClientNode *)node->cn_Node.ln_Succ;
  155.     }
  156.  
  157. return( node );
  158. }
  159.  
  160.  
  161. void RemoveClient( UWORD clientNumber, char *clientName )
  162. {
  163. struct ClientNode *node;
  164.  
  165. node = FindClientNode( clientNumber );
  166.  
  167. if ( node != (struct ClientNode *)ClientsList.lh_Head )
  168.     {
  169.     DetachGadgets();
  170.     Remove( (struct Node *)node );
  171.     FreeMem( node, sizeof(struct ClientNode) );
  172.     TotalClients--;
  173.  
  174.     if ( clientNumber < TotalClients )
  175.         DefaultClient = clientNumber;
  176.     else
  177.         DefaultClient = TotalClients;
  178.  
  179.     AttachGadgets();
  180.     }
  181. }
  182.  
  183.  
  184. void SendCommandToClient( ULONG command )
  185. {
  186. struct ServerMessage *bmsg;
  187.  
  188. if ( clientMPort && (bmsg = AllocServerMessage( command )))
  189.     {
  190.     PutMsg( clientMPort, (struct Message *)bmsg );
  191.     do
  192.         WaitPort( ServerPort );
  193.     while ( !(GetMsg( ServerPort )) );
  194.  
  195.     FreeServerMessage( bmsg );
  196.     }
  197. }
  198.  
  199.  
  200. void HandleClients( void )
  201. {
  202. struct ClientMessage *bmsg;
  203.  
  204. while( bmsg = (struct ClientMessage *)GetMsg( ServerPort ) )
  205.     {
  206.     switch ( bmsg->cm_Action )
  207.         {
  208.         case ACTION_ARRIVED:
  209.             if ( !Blanking )
  210.                 {
  211.                 clientMPort = bmsg->cm_Msg.mn_ReplyPort;
  212.                 bmsg->DInfo = AllocDisplayIDInformation( DisplayID );
  213.                 blankingAction = BLANKING_EXTERN;
  214.                 BlankStatus = MUST_BLANK;
  215.                 Blanking = TRUE;
  216.                 }
  217.             break;
  218.         case ACTION_FAILED:
  219.             if ( Blanking )
  220.                 {
  221.                 if ( copy_of_RandomClient != 0 )
  222.                     {
  223.                     copy_of_RandomClient = 0;
  224.                     copy_of_DefaultClient = TotalClients;
  225.                     }
  226.                 else
  227.                     copy_of_DefaultClient--;
  228.                     StartBlanking();
  229.                 }
  230.         default:
  231.             break;
  232.         }
  233.     ReplyMsg( (struct Message *)bmsg );
  234.     }
  235. }
  236.  
  237.  
  238. struct MsgPort *SetUpServerPort( void )
  239. {
  240. if ( ServerPort = CreatePort( SERVERPORTNAME, 0 ) )
  241.     serverSignal = 1L << ServerPort->mp_SigBit;
  242. return( ServerPort );
  243. }
  244.  
  245.  
  246. void RemoveServerPort( void )
  247. {
  248. DeletePort( ServerPort );
  249. }
  250.  
  251.  
  252.  /*****************
  253.  *                *
  254.  * Blanking stuff *
  255.  *                *
  256.  *****************/
  257.  
  258. UWORD ActiveClient;
  259.  
  260. void StartBlanking( void )
  261. {
  262. ULONG secs, micr;
  263.  
  264. clientMPort = NULL;
  265.  
  266. if ( !Blanking )
  267.     {
  268.     if ( copy_of_DefaultClient == ~0 )
  269.         {
  270.         copy_of_DefaultClient = DefaultClient;
  271.         copy_of_RandomClient = RandomClient;
  272.         }
  273.  
  274.     if ( window )
  275.         ClearPendingMessages( window->UserPort );
  276.  
  277.     if ( TotalClients && copy_of_RandomClient )
  278.         {
  279.         CurrentTime( &secs, &micr );
  280.         ActiveClient = secs % TotalClients + 1;
  281.         }
  282.     else    ActiveClient = copy_of_DefaultClient;
  283.  
  284.     if ( ActiveClient == 0 )
  285.         {
  286.         if ( PopUpBlackScreen() )
  287.             {
  288.             blankingAction = BLANKING_BUILTIN;
  289.             Blanking = TRUE;
  290.             }
  291.         else
  292.             {
  293.             DisplayBeep( NULL );
  294.             blankingAction = BLANKING_NONE;
  295.             Blanking = TRUE;
  296.             }
  297.         }
  298.     else
  299.         {
  300.         if ( !(StartClient( FindClientNode( ActiveClient ) ) ) )
  301.             {
  302.             copy_of_DefaultClient = 0;
  303.             Blanking = TRUE;
  304.             StartBlanking();
  305.             }
  306.         else
  307.             /* Blanking = TRUE <=> ACTION_ARRIVED */
  308.             blankingAction = BLANKING_EXTERN;
  309.         }
  310.     }
  311. }
  312.  
  313.  
  314. void StopBlanking( void )
  315. {
  316. if ( blankingAction == BLANKING_BUILTIN )
  317.     CloseBlackScreen();
  318. else
  319. if ( blankingAction == BLANKING_EXTERN )
  320.     SendCommandToClient( COMMAND_QUIT );
  321.  
  322. Blanking = FALSE;
  323. blankingAction = BLANKING_NONE;
  324.  
  325. copy_of_DefaultClient = (UWORD)~0;
  326. copy_of_RandomClient = (UWORD)~0;
  327. }
  328.  
  329.  
  330.  /*******************
  331.  *                  *
  332.  * PROCESS MESSAGES *
  333.  *                  *
  334.  *******************/
  335.  
  336. void ProcessUserMessages(void)
  337. {
  338. ULONG signal;
  339.  
  340. while( StayCool )
  341.     {
  342.     signal = Wait( CXSignal | windowSignal | serverSignal | handlerSignal | timerSignal );
  343.  
  344.     if ( signal & CXSignal )
  345.         HandleCxMessages();
  346.  
  347.     if ( signal & windowSignal )
  348.         HandleWindowMessages();
  349.  
  350.     if ( signal & serverSignal )
  351.         HandleClients();
  352.  
  353.     if ( signal & timerSignal && CommodityActive )
  354.         StartBlanking();
  355.  
  356.     if ( signal & handlerSignal )
  357.         StopBlanking();
  358.     }
  359.  
  360. if ( Blanking )
  361.     StopBlanking();
  362. }
  363.  
  364.  
  365.  /***************
  366.  *              *
  367.  * MAIN PROGRAM *
  368.  *              *
  369.  ***************/
  370.  
  371. #define POP_KEY_ID      100
  372.  
  373. extern CxObj *broker , *sender, *translate;
  374. CxObj *hotkeyfilter;
  375. CxObj *mousefilter, *timerfilter, *keyfilter;
  376.  
  377. extern struct NewBroker newbroker;
  378. extern struct MsgPort *broker_mp;
  379.  
  380.  
  381. void __interrupt __saveds BlankerAction(CxMsg *CxMsg,CxObj *CO)
  382. {
  383. struct InputEvent *IE=(struct InputEvent *)CxMsgData(CxMsg);
  384.  
  385. if (IE->ie_Class==IECLASS_TIMER)
  386.     {
  387.     if ( delaySecs && ++timeElapsed >= delayEvents && BlankStatus != MUST_BLANK )
  388.         {
  389.         BlankStatus = MUST_BLANK;
  390.         Signal( thisTask, timerSignal );
  391.         }
  392.     }
  393. else
  394.     {
  395.     timeElapsed = 0;
  396.     if ( BlankStatus != MUST_RETRY )
  397.         {
  398.         BlankStatus = MUST_RETRY;
  399.         Signal( thisTask, handlerSignal );
  400.         }
  401.     }
  402. }
  403.  
  404.  
  405. extern char wname[100];
  406.  
  407. void main( int argc, char *argv[] )
  408. {
  409. struct ClientNode *firstNode;
  410. CxObj *customobj;
  411. char hotkey[100];
  412. char ListName[100];
  413. struct List *list;
  414. UWORD len;
  415.  
  416. if ( IntuitionBase = (struct IntuitionBase *)OpenLibrary( "intuition.library", 37L ) )
  417.     {
  418.     if ( UtilityBase = OpenLibrary( "utility.library", 37L ) )
  419.         {
  420.         if ( argc )
  421.             {
  422.             struct RDArgs *rdargs;
  423.             LONG argres[8] = { 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L };
  424.  
  425.             if ( rdargs = ReadArgs( "CX_PRIORITY/K/N,CX_POPUP/S,CX_HOTKEY/K,INACTIVE/S,RANDOM/S,TIMEOUT/K/N,DISPLAY/K,LIST/K", argres, NULL ) )
  426.                 {
  427.                 newbroker.nb_Pri =  ( argres[0] ? *(ULONG *)argres[0] : 0 );
  428.                 PopUp = ( argres[1] ? TRUE : FALSE );
  429.                 strcpy( hotkey, ( argres[2] ? (char *)argres[2] : "lalt b" ) );
  430.                 CommodityActive = ( argres[3] ? FALSE : TRUE );
  431.                 RandomClient = ( argres[4] ? TRUE : FALSE );
  432.                 delaySecs = ( argres[5] ? *(ULONG *)argres[5] : 60 );
  433.                 if ( argres[6] )
  434.                     strcpy( DefaultModeName, (char *)argres[6] );
  435.                 strcpy( ListName, ( argres[7] ? (char *)argres[7] : "ClientList" ) );
  436.                 FreeArgs( rdargs );
  437.                 }
  438.             }
  439.         else
  440.         if ( IconBase = OpenLibrary( "icon.library", 37L ) )
  441.             {
  442.             char **tooltypes = (char **)ArgArrayInit( argc, argv );
  443.             newbroker.nb_Pri = ArgInt( tooltypes, "CX_PRIORITY", 0 );
  444.             strcpy( hotkey, ArgString( tooltypes, "CX_HOTKEY", "lalt b" ) );
  445.             delaySecs = ArgInt( tooltypes, "TIMEOUT", 3 );
  446.             strcpy( ListName, ArgString( tooltypes, "LIST", "ClientList" ) );
  447.  
  448.             PopUp =
  449.                 !Stricmp(ArgString(tooltypes, "CX_POPUP", "NO"), "YES" );
  450.             CommodityActive =
  451.                 !Stricmp(ArgString(tooltypes, "ACTIVE", "YES"), "YES" );
  452.             RandomClient =
  453.                 !Stricmp(ArgString(tooltypes, "RANDOM", "YES"), "YES" );
  454.             strcpy( DefaultModeName, ArgString( tooltypes, "DISPLAY", NULL ) );
  455.  
  456.             ArgArrayDone();
  457.             CloseLibrary( IconBase );
  458.             }
  459.         CloseLibrary( UtilityBase );
  460.         }
  461.  
  462.     if ( list = CreateModeList() )
  463.         {
  464.         GetDisplayNodeFromName();
  465.         DeleteModeList();
  466.         }
  467.  
  468.     delayEvents = delaySecs * 10;
  469.  
  470.     if ( CxBase = OpenLibrary( "commodities.library", 37L ) )
  471.         {
  472.         if ( GadToolsBase = OpenLibrary( "gadtools.library", 0L ) )
  473.             {
  474.             if ( broker_mp = CreateMsgPort() )
  475.                 {
  476.                 CXSignal = 1L << broker_mp->mp_SigBit;
  477.                 newbroker.nb_Port = broker_mp;
  478.  
  479.                 if ( broker = CxBroker( &newbroker, NULL ) )
  480.                     {
  481.                     if ( (handlerSigBit = AllocSignal( -1 )) != -1 )
  482.                         {
  483.                         handlerSignal = 1L << handlerSigBit;
  484.  
  485.                         if ( (timerSigBit = AllocSignal( -1 )) != -1 )
  486.                             {
  487.                             timerSignal = 1L << timerSigBit;
  488.  
  489.                             thisTask = FindTask( NULL );
  490.  
  491.                             if ( customobj = CxCustom( BlankerAction, 0L ) )
  492.                                 {
  493.                                 AttachCxObj( broker, customobj );
  494.  
  495.                                 if (hotkeyfilter = HotKey(hotkey, broker_mp, 1L ) )
  496.                                     {
  497.                                     AttachCxObj( broker, hotkeyfilter );
  498.                                     strcpy(wname, PROGRAMNAME": HotKey = <");
  499.                                     len = sizeof(PROGRAMNAME": HotKey = <") - 1;
  500.                                     strncpy(wname + len, hotkey, 100 - len);
  501.                                     len = strlen(wname);
  502.                                     strncpy(wname + len, ">", 100 - len);
  503.                                     }
  504.  
  505.                                 if ( !CxObjError( hotkeyfilter ) )
  506.                                     {
  507.                                     if ( firstNode = AllocVec( sizeof(struct ClientNode), MEMF_CLEAR ) )
  508.                                         {
  509.                                         NewList( &ClientsList );
  510.                                         firstNode->cn_Node.ln_Name = "Builtin blanker";
  511.                                         AddTail( &ClientsList, (struct Node *)firstNode );
  512.  
  513.                                         if ( SetUpServerPort() )
  514.                                             {
  515.                                             GetClientNames( ListName );
  516.  
  517.                                             if ( PopUp )
  518.                                                 PopUpWindow();
  519.  
  520.                                             ActivateCxObj( broker, 1L );
  521.                                             ProcessUserMessages();
  522.                                             ShutWindow();
  523.                                             DropClientNames();
  524.                                             DeletePort( ServerPort );
  525.                                             }
  526.                                         else
  527.                                         FreeVec( firstNode );
  528.                                         }
  529.                                     }
  530.                                 }
  531.                             FreeSignal( timerSigBit );
  532.                             }
  533.                         FreeSignal( handlerSigBit );
  534.                         }
  535.                     DeleteCxObjAll( broker );
  536.                     }
  537.                 DeleteMsgPort( broker_mp );
  538.                 }
  539.             CloseLibrary( GadToolsBase );
  540.             }
  541.         CloseLibrary( CxBase );
  542.         }
  543.     CloseLibrary( (struct Library *)IntuitionBase );
  544.     }
  545. }
  546.